<?php $db = new Mypdo(); ?>
<?php



if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

    if(!empty($_POST)){




        $photoManager = new PhotoManager($db);

        $allowedExtsImage = array("jpg", "jpeg", 'png', "JPG", "JPEG",'PNG');
        $dossier_image = './image/upload/';
        $tailleImage = 10000000;



        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        /*
        echo $_FILES["file"]["name"]."</br>";
        echo $_FILES["file"]["type"]."</br>";
        echo $_FILES["file"]["size"]."</br>";
        echo $fichier
        echo $extension."</br>";
        */
        $taille = ((($_FILES["file"]["size"])/1024)/1024);
        //$chemin = $dossier_image . $fichier;


        if ((($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "application/pdf"))
            && ($_FILES["file"]["size"] < $tailleImage)
            && in_array($extension, $allowedExtsImage))
        {
            if ($_FILES["file"]["error"] > 0)
            {
                echo "Chargement imposible";
                //echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            }
            else
            {/*
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
            */
                if (file_exists($dossier_image. $_FILES["file"]["name"]))
                {
                    echo $_FILES["file"]["name"] . " existe déjà. ";
                }
                else
                {

                    $id_loc = $_GET['id'];
                    $id_photo = $photoManager->getNbPhoto($id_loc);

                    $id_photo = empty($id_photo)?1:$id_photo+1;
                    $info = new SplFileInfo($_FILES["file"]["name"]);
                    $fichier = $id_loc.'-'.$id_photo.'.'.$info->getExtension();

                    $photo = new Photo(array('id_photo'=>$id_photo,'id_loc'=>$id_loc,'lien_photo'=>$fichier,'descp_photo'=>$_POST['descp_photo']));

                    $photoManager->add($photo);
                    move_uploaded_file($_FILES["file"]["tmp_name"],
                        $dossier_image . $fichier);

                    echo 'Chargement Terminer';
                    //echo 'Stored in: ' . $dossier_image . $fichier;
                    header('Location: index.php?page=113&id='. $_GET['id']);

                }
            }
        }
        else
        {
            echo "Fichier Invalide";
        }
    }else{
?>
        <form method="post" action="#" enctype="multipart/form-data">

        <p>
        <h1>Chargement d'une image</h1>
        <br/>



            <label for="file">Images (jpg, pdf): </label>
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
            <input type="file" name="file" id="file" required><br/>
            <br/>
        <label for="descp_photo">Description :</label><textarea rows="4" cols="30" name="descp_photo" id="descp_photo" maxlength="255"  ></textarea>


        </p>

        <p><input type="reset" value="Annuler" /><input type="submit" value="Valider" /></p>
        </form>
    <?php }
}else{
    header('Location: index.php');
}